package com.deer.wms.system.manage.web;

import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.util.NetworkUtil;
import com.deer.wms.project.root.util.RedisUtil;
import com.deer.wms.system.manage.model.permission.Menu;
import com.deer.wms.system.manage.model.user.UserDetail;
import com.deer.wms.system.manage.model.user.UserLogin;
import com.deer.wms.system.manage.service.LoginService;
import com.deer.wms.system.manage.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by guo on 2017/9/16.
 */
@Api(description = "用户登录api接口")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private PermissionService permissionService;

    /**
     * 用户登录
     *
     * @param login
     * @return
     */
    @ApiOperation(value = "用户登录", notes = "用户登录")
    @PostMapping("/info")
    public Result userLogin(@RequestBody UserLogin login, HttpServletRequest request) {
        /* String macNum = MacUtil.getLocatMac();
        if(!macNum.equals("60-30-D4-65-C4-70") ){
            return ResultGenerator.genFailResult(CommonCode.ILLEGAL_LANDING,null);
        }*/
        String ip = NetworkUtil.getIpAddress(request);
//        login.setIp("122.112.219.208");
        login.setIp(ip);
        UserDetail detail = loginService.login(login, request);
        List<Menu> menus = detail.getMenus();
        List<Menu> menus1 =  new ArrayList<Menu>();
        List<Menu> menus2 = new ArrayList<Menu>();
        List<Menu> menus3 = new ArrayList<Menu>();
        for(Menu menu :menus){
            if(menu.getResourceGroup() !=null){
                if(menu.getResourceGroup() ==1){
                    menus1.add(menu);
                }
                if(menu.getResourceGroup() ==2){
                    menus2.add(menu);
                }
                if(menu.getResourceGroup() ==3){
                    menus3.add(menu);
                }

            }

        }

        detail.setMenus(null);
        detail.setMenus1(menus1);
        detail.setMenus2(menus2);
        detail.setMenus3(menus3);
        return ResultGenerator.genSuccessResult(detail);
    }


    /**
     * 根据账号--获取菜单
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "用户登录", notes = "用户登录")
    @GetMapping("/PDAGetMenus")
    public Result PDAGetMenus(Integer userId) {
        //获取用户菜单
        List<Menu> menus = permissionService.findPermissionMenuByUserId(userId);
        List<Menu> pdaMenu = new ArrayList<>();
        //这里只会遍历 4-PDA 菜单资源
        for (Menu menu : menus) {
            if (menu.getResourceGroup() != null) {
                //这里去除掉PDA菜单的目录
                if (menu.getResourceGroup() == 4 && menu.getParentId() != null) {
                    pdaMenu.add(menu);
                }
            }
        }
        return ResultGenerator.genSuccessResult(pdaMenu);
    }

    @GetMapping("/findOnlineUsers")
    public Result userLogin2( ) {
        List<Object> list =  redisUtil.findByPattern("AccessToken*");
        List<CurrentUser> currentUsers = new ArrayList<>();
        list.forEach(o->{
            currentUsers.add((CurrentUser)o);
        });

        return ResultGenerator.genSuccessResult(currentUsers);
    }



}